package cn.gzsendi;

import cn.gzsendi.websocket.service.RobotService;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

//测试地址：http://ip:8081/remotewin?accessToken=123456
//或者 http://ip:8081/page/remotewin.html?accessToken=123456
//可以在虚机里面启动服务，然后在电脑本机进行测试
//注意：本程序要求服务器不能在本机, 不能在本地127.0.0.1测试，在本地电脑测试会产生镜中镜的效果，无限下去。
@SpringBootApplication
public class WebApplication {

    public static void main(String[] args) {

        SpringApplicationBuilder builder = new SpringApplicationBuilder(WebApplication.class);
        ConfigurableApplicationContext ctx = builder.headless(false).run(args);
        Environment env = ctx.getEnvironment();
        InetAddress candidateAddress = null;
        try {
            Enumeration<NetworkInterface> networkInterfaces = null;
            networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface iface = networkInterfaces.nextElement();
                // 该网卡接口下的ip会有多个，也需要一个个的遍历，找到自己所需要的
                for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) {
                    InetAddress inetAddr = inetAddrs.nextElement();
                    // 排除loopback回环类型地址（不管是IPv4还是IPv6 只要是回环地址都会返回true）
                    if (!inetAddr.isLoopbackAddress()) {
                        if (inetAddr.isSiteLocalAddress()) {
                            // 如果是site-local地址，就是它了 就是我们要找的
                            // ~~~~~~~~~~~~~绝大部分情况下都会在此处返回你的ip地址值~~~~~~~~~~~~~
                            candidateAddress = inetAddr;
                        }
                        // 若不是site-local地址 那就记录下该地址当作候选
                        if (candidateAddress == null) {
                            candidateAddress = inetAddr;
                        }
                    }
                }
            }
            String ip = candidateAddress.getHostAddress();
            String port = env.getProperty("server.port");
            String property = env.getProperty("server.servlet.context-path");
            String path = property == null ? "" : property;
            System.out.println(
                    "\n\t" +
                            "----------------------------------------------------------\n\t" +
                            "Application DDR Controller is running!\n\t" +
                            "IP: \t" + ip + " \n\t" +
//                            "Web: \thttp://" + ip + ":" + port + path + "remotewin?accessToken=123456\n\t" +
                            "------------------------------------------------------------");

            RobotService robotService = ctx.getBean(RobotService.class);
//		robotService.startCaputureTask();
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }
    }
}
